//
//  UILabel+Extension.m
//  GongDan
//
//  Created by 了凡 on 16/1/20.
//  Copyright © 2016年 河北广联. All rights reserved.
//

#import "UILabel+Extension.h"

@implementation UILabel (Extension)

+ (UILabel *)labelWithTextColor:(UIColor *)textColor font:(CGFloat)textFont backgroundColor:(UIColor *)backColor Text:(NSString *)text
{
    UILabel *label = [[UILabel alloc]init];
    //文字
    label.text = text;
    //文字颜色
    label.textColor = textColor;
    //字体
    label.font = [UIFont systemFontOfSize:textFont];
    //背景色
    if (backColor != nil) {
        label.backgroundColor = backColor;
    }
    return label;
}

- (instancetype)initWithFrame:(CGRect)frame
                         font:(UIFont *)font
                    textColor:(UIColor *)color {
    return [self initWithFrame:frame
                          font:font
                     textColor:color
                 textAlignment:NSTextAlignmentLeft];
}

- (instancetype)initWithFrame:(CGRect)frame
                         font:(UIFont *)font
                    textColor:(UIColor *)color
                textAlignment:(NSTextAlignment)alignment {
    self = [super initWithFrame:frame];
    if (self) {
        self.font = font;
        self.textAlignment = alignment;
        self.enabled = YES;
        self.textColor = color;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    
    NSString *labelText = label.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
    
}

+ (void)changeAligentLabel:(UILabel *)label withLabelWith:(CGFloat)labelWidth withFont:(UIFont *)font
{
    //两端对齐
    CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesFontLeading  attributes:@{NSFontAttributeName :font} context:nil].size;
    CGFloat margin = (labelWidth - textSize.width)/(label.text.length - 1);
    NSNumber *number = [NSNumber numberWithFloat:margin];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:label.text];
    //字间距 :NSKernAttributeName
    [attribute addAttribute:NSKernAttributeName value:number range:NSMakeRange(0, label.text.length - 1)];
    label.attributedText = attribute;
    
}


@end
